<script setup lang="ts">
import px2rem from '@/utils/flexble'
import { computed } from 'vue'
import { client } from '@/utils/http'
import { useQuery } from '@tanstack/vue-query'
import useFilterStore from '@/stores/filter'
import { storeToRefs } from 'pinia'
import { segmentHeight } from '@/utils/lib'
import { toPercent, toThousand } from '@/utils/filter'
import { dropRight } from 'lodash-es'

const filterStore = useFilterStore()

const { ymdId, checkDataUpdateNum } = storeToRefs(filterStore)

const { data, isFetching } = useQuery<{
  clueToShopDlr: string
  orderToSaleDlr: string
  testDriveToOrderCountry: string
  clueColor: string
  clueDlrColor: string
  testDriveToOrderRegion: string
  clueQty: number
  shopToTestDriveRegion: string
  toShopColor: string
  toShopDlrColor: string
  testDriveToOrderDlr: string
  clueToShopCountry: string
  newOrderQty: number
  saleColor: string
  testDriveQty: number
  shopToTestDriveCountry: string
  toShopQty: number
  shopToTestDriveDlr: string
  testDriveColor: string
  testDriveDlrColor: string
  newOrderColor: string
  newOrderDlrColor: string
  orderToSaleRegion: string
  saleQty: number
  clueToShopRegion: string
  orderToSaleCountry: string
}>({
  keepPreviousData: true,
  queryKey: ['/saleCoreBoard/getSalesFunnel', ymdId, checkDataUpdateNum],
  queryFn: () => client('/saleCoreBoard/getSalesFunnel', { ymdId: ymdId.value }),
  enabled: computed(() => !!ymdId.value)
})

const funnelDatas = computed(() => {
  if (!data.value) {
    return []
  }
  const {
    clueQty,
    clueColor,
    clueDlrColor,
    clueToShopDlr,
    clueToShopRegion,
    clueToShopCountry,

    toShopQty,
    toShopColor,
    toShopDlrColor,
    shopToTestDriveDlr,
    shopToTestDriveRegion,
    shopToTestDriveCountry,

    testDriveQty,
    testDriveColor,
    testDriveDlrColor,
    testDriveToOrderDlr,
    testDriveToOrderRegion,
    testDriveToOrderCountry,

    newOrderQty,
    newOrderColor,
    newOrderDlrColor,
    orderToSaleDlr,
    orderToSaleRegion,
    orderToSaleCountry,

    saleQty,
    saleColor
  } = data.value
  return [
    {
      label: '线上线索',
      isGreen: clueColor === 'green',
      isValueGreen: clueDlrColor === 'green',
      value: toThousand(clueQty),
      value1: toPercent(clueToShopDlr, 1, false, '%', 1),
      value2: toPercent(clueToShopRegion, 1, false, '%', 1),
      value3: toPercent(clueToShopCountry, 1, false, '%', 1)
    },
    {
      label: '到店组数',
      isGreen: toShopColor === 'green',
      isValueGreen: toShopDlrColor === 'green',
      value: toThousand(toShopQty),
      value1: toPercent(shopToTestDriveDlr, 1, false, '%', 1),
      value2: toPercent(shopToTestDriveRegion, 1, false, '%', 1),
      value3: toPercent(shopToTestDriveCountry, 1, false, '%', 1)
    },
    {
      label: '试乘试驾',
      isGreen: testDriveColor === 'green',
      isValueGreen: testDriveDlrColor === 'green',
      value: toThousand(testDriveQty),
      value1: toPercent(testDriveToOrderDlr, 1, false, '%', 1),
      value2: toPercent(testDriveToOrderRegion, 1, false, '%', 1),
      value3: toPercent(testDriveToOrderCountry, 1, false, '%', 1)
    },
    {
      label: '新增订单',
      isGreen: newOrderColor === 'green',
      isValueGreen: newOrderDlrColor === 'green',
      value: toThousand(newOrderQty),
      value1: toPercent(orderToSaleDlr, 1, false, '%', 1),
      value2: toPercent(orderToSaleRegion, 1, false, '%', 1),
      value3: toPercent(orderToSaleCountry, 1, false, '%', 1)
    },
    {
      label: '销售',
      isGreen: saleColor === 'green',
      value: toThousand(saleQty)
    }
  ]
})

const tableDatas = computed(() => {
  return dropRight(funnelDatas.value)
})
</script>

<template>
  <Segment :height="segmentHeight" title="销售漏斗" :loading="isFetching">
    <div class="sales-funnel">
      <div class="funnel">
        <div
          class="item"
          v-for="(funnel, index) in funnelDatas"
          :key="funnel.label"
          :class="{ green: funnel.isGreen }"
          :style="{ width: `calc(100% - ${index ? px2rem(30 * index) : '0px'})` }"
        >
          <div class="item-bg" />
          <div class="item-value">
            <div class="label">{{ funnel.label }}</div>
            <div class="value">{{ funnel.value }}</div>
            <div class="line" :class="[`line-${index}`]" />
          </div>
        </div>
      </div>
      <div class="table">
        <div class="header">
          <div>自店</div>
          <div>区域</div>
          <div>全国</div>
        </div>
        <div class="item" v-for="(item, index) in tableDatas" :key="index">
          <div class="values">
            <div
              class="value"
              :class="{ 'red-color': !item.isValueGreen, 'green-color': item.isValueGreen }"
            >
              {{ item.value1 }}
            </div>
            <div class="value">{{ item.value2 }}</div>
            <div class="value">{{ item.value3 }}</div>
          </div>
        </div>
        <div class="item"></div>
      </div>
    </div>
  </Segment>
</template>

<style lang="less" scoped>
.sales-funnel {
  height: calc(100% - 32px);
  padding: 10px 10px 18px;
  display: flex;
  .funnel {
    height: 100%;
    flex: 1.4;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-right: 32px;
    .item {
      margin-bottom: 8px;
      position: relative;
      // flex: 1;
      height: 20%;
      display: flex;
      align-items: center;
      flex-direction: column;
      justify-content: center;
      .item-bg {
        position: absolute;
        height: 100%;
        left: 24px;
        right: 24px;
        background: linear-gradient(to bottom, #90301b 0%, #051035 100%);
      }
      .item-value {
        position: absolute;
        width: 100%;
        height: 100%;
        display: flex;
        align-items: center;
        flex-direction: column;
        justify-content: center;
        &::before {
          content: '';
          position: absolute;
          top: 0;
          right: 0;
          bottom: 0;
          width: 30px;
          background: url('@/assets/icons/funnel-red-bg.svg') no-repeat right;
          background-size: cover;
          background-position: calc(100% - 9px) 0;
        }
        &::after {
          content: '';
          position: absolute;
          top: 0;
          left: 0;
          bottom: 0;
          width: 30px;
          background: url('@/assets/icons/funnel-red-bg.svg') no-repeat left;
          background-size: cover;
          background-position: 9px 0;
        }
      }
      &.green {
        .item-bg {
          background: linear-gradient(to bottom, #1a713d 0%, #051035 100%);
        }
        .item-value {
          &::before {
            background: url('@/assets/icons/funnel-green-bg.svg') no-repeat right;
            background-size: cover;
            background-position: calc(100% - 9px) 0;
          }
          &::after {
            background: url('@/assets/icons/funnel-green-bg.svg') no-repeat left;
            background-size: cover;
            background-position: 9px 0;
          }
        }
      }
      &:last-child {
        &::after {
          display: none;
        }
        .line {
          display: none;
        }
      }
      &::after {
        content: '';
        position: absolute;
        z-index: 9;
        width: 44px;
        height: 56px;
        background: url('@/assets/icons/funnel-exchange.svg') no-repeat;
        right: -16px;
        bottom: -34px;
        background-size: cover;
      }
      .line {
        position: absolute;
        z-index: -1;
        width: 100px;
        height: 1px;
        background: linear-gradient(
          to right,
          rgba(46, 78, 179, 1) 0%,
          rgba(46, 78, 179, 0.05) 100%
        );
        left: calc(100% + 14px);
        bottom: -2px;
        &.line-0 {
          width: 16px;
        }
        &.line-1 {
          width: 40px;
        }
        &.line-2 {
          width: 60px;
        }
        &.line-3 {
          width: 80px;
        }
      }
      .label {
        color: #fff;
        text-align: center;
        font-family: Alibaba PuHuiTi 2;
        font-size: 14px;
        font-style: normal;
        font-weight: 400;
        line-height: normal;
      }
      .value {
        color: #fff;
        font-family: DIN Alternate;
        font-size: 18px;
        font-style: normal;
        font-weight: 700;
        line-height: normal;
      }
    }
  }
  .table {
    position: relative;
    flex: 1;
    height: 100%;
    background: #050f32;
    display: flex;
    flex-direction: column;
    align-items: center;
    .header {
      margin-top: 10px;
      position: absolute;
      display: flex;
      align-items: center;
      width: 100%;
      div {
        flex: 1;
        color: #799bc0;
        text-align: center;
        font-family: Alibaba PuHuiTi 2;
        font-size: 14px;
        font-style: normal;
        font-weight: 400;
        line-height: 16px; /* 114.286% */
      }
    }
    .item {
      transform: translateY(50%);
      width: 100%;
      margin-bottom: 8px;
      // flex: 1;
      height: 25%;
      padding: 8px 0;
      .values {
        height: 100%;
        // padding: 13px 0;
        display: flex;
        align-items: center;
        border-radius: 8px;
        background: linear-gradient(270deg, rgba(18, 38, 89, 0) 0%, rgba(11, 48, 120, 0.48) 100%);
        .value {
          flex: 1;
          // padding: 13px 0;
          color: #fff;
          text-align: center;
          font-family: Alibaba PuHuiTi 2;
          font-size: 14px;
          font-style: normal;
          font-weight: 600;
          line-height: 16px; /* 114.286% */
          &.red-color {
            color: #ff5656;
          }
          &.green-color {
            color: #35c08e;
          }
        }
      }
    }
  }
}
</style>
